--
-- Author: ping.wang
-- Date: 2019-03-12 16:58:00
--
-- PvpDuelFightEndView
--
local IconRender = require("app.widget.renders.IconRender")
local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)
local duelGreen = cc.c3b(156,255,0)
local duelOrange = cc.c3b(255,192,0)

ClassRef.VIEW_ROOT_RES = Res.PvpDuelFightEndWinScene

function ClassRef:init()
	self._ui.Button_close:addClickEventListener(function()
		self:closeView()
	end)

	--若是新纪录

	self._ui.tips_get:setVisible(false)

	--文本要设置为奖励
	self._ui.tips_3:setVisible(true)
	self._ui.tips_3:setString(L("duelGametips25"))
	self._ui.tlt:setString(L("duelGametips13"))
	local upNum = self._data.rankChange
	if upNum > 0 then
		self._ui.tips_1:setString(string.format(L("duelGametips22"),upNum))
	else
		self._ui.tips_1:setString(L("duelGametips23"))
	end

	if self._data.newRecord then
		self._ui.tips_new:setVisible(true)
	else
		self._ui.tips_new:setVisible(false)
	end

	--此处区分了排名奖和普通奖
	local rankRewards = self._data.rankChangeReward
	local addStartIdx = 0
	if rankRewards then
		for i = 1, #rankRewards do
			local itemData = UD:parseLootToBagItem( rankRewards[i] )
			local render = IconRender.createWithData( itemData, self._ui["icon_" .. i] )
			render:setTipEnable( true )
			render:setTipUnShowOrigin( true )
			local text = self._ui["icon_" .. i]:findChild("Text")
			text:setString(L("duelGametips15"))
			text:setTextColor(duelOrange)
		end
		addStartIdx = #rankRewards
	end


	local rewards = self._data.loot
	for i,reward in ipairs(rewards) do
		local idx = i+addStartIdx
		local itemData = UD:parseLootToBagItem( reward )
		local render = IconRender.createWithData( itemData, self._ui["icon_" .. idx] )
		render:setTipEnable( true )
		render:setTipUnShowOrigin( true )
		local text = self._ui["icon_" .. idx]:findChild("Text")
		text:setString(L("duelGametips13"))
		text:setTextColor(duelGreen)
	end

	self:_autoAdjustRewardPanel(#rewards + addStartIdx)
end

-- 自动调整奖励panel中奖励posX
function ClassRef:_autoAdjustRewardPanel(visibleNum)
	for i = 1, 4 do
		self._ui["icon_" .. i]:setVisible(i <= visibleNum)
	end
	local width = self._ui.Panel:getContentSize2()
	if 1 == visibleNum then
		self._ui.icon_1:setPositionX(width * 0.5 - 38)
	elseif 2 == visibleNum then
		self._ui.icon_1:setPositionX( width*0.35 - 38) -- 常量系数是UI中的posX比例
		self._ui.icon_2:setPositionX( width*0.65 - 38)
	elseif 3 == visibleNum then
		self._ui.icon_1:setPositionX( width*0.25 - 38) -- 常量系数是UI中的posX比例
		self._ui.icon_2:setPositionX( width*0.5 - 38)
		self._ui.icon_3:setPositionX( width*0.75 - 38)
	end
end

return ClassRef
